package com.singing.manager;

import com.singing.dao.SingerDao;
import com.singing.model.Command;
import com.singing.model.Singer;

import java.util.List;
import java.util.Scanner;

public class SingerRemoveManager extends BaseManager {


    private static final int REFRESH_PAGE = 1;
    // 删除歌手
    private static final int REMOVE_SINGER = 2;
    SingerDao singerDao = new SingerDao();

    public SingerRemoveManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected int processCommand(int command) {
        switch (command) {
            case REFRESH_PAGE:
                break;
            case REMOVE_SINGER:
                removeSinger();
                break;
        }
        return command;
    }

    private void removeSinger() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要删除的歌手id:");
            String idStr = scanner.next();

            // 判断是否是数字
            try {
                int id = Integer.parseInt(idStr);
                int count = singerDao.deleteById(id);
                if (count <= 0) {
                    System.out.println("对应歌手不存在，请重新输入");
                } else {
                    System.out.println("删除成功");
                    break;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                System.out.println("歌手id输入错误，请重新输入");
            }

        }
    }

    @Override
    protected void printTitleExtra() {
        System.out.println("歌手id\t\t歌手名称\t\t歌手性别");
        showSingerList();
    }

    private void showSingerList() {
        List<Singer> singers = singerDao.selectAll();
        Scanner scanner = new Scanner(System.in);
        for (Singer singer : singers) {
            System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + (singer.getGender() == 0 ? "男" : "女"));
        }
    }
}
